#!/usr/bin/env python
# encoding:utf-8

from utils import *

context.log_level = 'warn'

def check(s):
    io = remote('202.120.7.144', 9994)
    # io = process('python ./loader.py'.split(' '))
    io.sendline('')
    io.recvuntil('op:')
    io.sendline('3')
    io.recvuntil('Passcode:\n')
    io.sendline(s)
    io.recv(len('passcode ') + len(s))
    t = io.recvuntil(' is', drop=True)[:4]
    # print t.__repr__()
    if t == '':
        result = 'fuck'
    else:
        result = unpack(t, 'all', signed=True)
    print s.__repr__(), io.recvline()
    io.close()
    print result
    return result

set_pad_char('\x01')
# now = 'xxxxxxx'
now = 'dGhpc3Bhc3Njb2R'
# for j in xrange(88, 123):
#     if j == ord('\n'):
#         continue
#     for i in xrange(48, 123):
#         if i == ord('\n'):
#             continue
#         fuck = check(right_pad(now + chr(j) + chr(i), 0x18))
#         print j, i, fuck
#         if fuck != 'fuck':
#             exit()
# exit()

while True:
    print '=' * 10, 'current: ', now
    if len(now) % 2 == (0 ^ int(len(now) > 7)):
        now += chr(1 - check(right_pad(now + '\x01', 0x18)))
        continue
    for i in xrange(1, 256):
        if i == ord('\n'):
            continue
        if check(right_pad(now + chr(i), 0x18)) != 'fuck':
            now += chr(i)
            break
